package com.ccrfid.rmc6.resource.vehicle;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.entity.vehicle.VehicleEntity;
import com.ccrfid.rmc6.entity.vehicle.VehicleEntryRecordView;
import com.ccrfid.rmc6.pojo.dto.LocatorAssetCount;
import com.ccrfid.rmc6.pojo.dto.vehicle.VehicleInfoDto;
import com.ccrfid.rmc6.pojo.dto.vehicle.VehicleMoveInLocationDto;
import com.ccrfid.rmc6.pojo.param.PageParam;
import com.ccrfid.rmc6.repository.search.Criterion;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.service.vehicle.VehicleService;
import com.ccrfid.rmc6.util.GeomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.sql.Timestamp;
import java.util.List;

@Path("vehicles")
@Produces(MediaType.APPLICATION_JSON)
public class VehicleResource {
    @Autowired
    private VehicleService vehicleService;

    //分页查询车辆列表
    @GET
    @Path("page")
    public Page<VehicleEntity> getVehiclePage(@QueryParam("cardNumber") Long cardNumber,
                                              @QueryParam("enabled") Integer enabled,
                                              @QueryParam("outside") Integer outside,
                                              @QueryParam("vehicleSn") String vehicleSn,
                                              @QueryParam("plateNumber") String plateNumber,
                                              @QueryParam("driverName") String driverName,
                                              @QueryParam("linkman") String linkman,
                                              @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (cardNumber != null) {
            criteria.add(new Criterion("cardNumber", Criterion.Operator.EQ, cardNumber));
        }
        if (enabled != null) {
            criteria.add(new Criterion("enabled", Criterion.Operator.EQ, enabled));
        }
        if (outside != null) {
            criteria.add(new Criterion("outside", Criterion.Operator.EQ, outside));
        }
        if (vehicleSn != null) {
            criteria.add(new Criterion("vehicleSn", Criterion.Operator.LIKE, '%' + vehicleSn + '%'));
        }
        if (plateNumber != null) {
            criteria.add(new Criterion("plateNumber", Criterion.Operator.LIKE, '%' + plateNumber + '%'));
        }
        if (driverName != null) {
            criteria.add(new Criterion("driverName", Criterion.Operator.LIKE, '%' + driverName + '%'));
        }
        if (linkman != null) {
            criteria.add(new Criterion("linkman", Criterion.Operator.LIKE, '%' + linkman + '%'));
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return vehicleService.getVehiclePage(criteria, pageRequest);
    }

    //获取单个车辆信息
    @GET
    @Path("{vehicleId}")
    public VehicleEntity getVehicleById(@PathParam("vehicleId") Long vehicleId) throws AppException {
        return vehicleService.getVehicleById(vehicleId);
    }

    @GET
    @Path("{cardNumber}/by-card-number")
    public VehicleEntity getVehicleByCardNumber(@PathParam("cardNumber") Long cardNumber) {
        return vehicleService.getVehicleByCardNumber(cardNumber);
    }

    //新增车辆信息
    @POST
    @Path("")
    @Consumes(MediaType.APPLICATION_JSON)
    public VehicleEntity createVehicle(VehicleEntity entity) throws AppException {
        return vehicleService.createVehicle(entity);
    }

    //更新车辆信息
    @PUT
    @Path("{vehicleId}")
    @Consumes(MediaType.APPLICATION_JSON)
    public VehicleEntity updateVehicle(@PathParam("vehicleId") Long vehicleId, VehicleEntity entity) throws AppException {
        return vehicleService.updateVehicle(vehicleId, entity);
    }

    //删除车辆信息
    @DELETE
    @Path("{vehicleId}")
    public Boolean deleteVehicle(@PathParam("vehicleId") Long vehicleId) throws AppException {
        return vehicleService.deleteVehicle(vehicleId);
    }

    //获取车辆图片
    @GET
    @Path("{vehicleId}/photo")
    @Produces("image/jpeg")
    public byte[] getVehiclePhoto(@PathParam("vehicleId") Long vehicleId) throws AppException {
        return vehicleService.getVehiclePhoto(vehicleId);
    }

    //更新车辆图片
    @PUT
    @Path("{vehicleId}/photo")
    @Consumes(MediaType.APPLICATION_OCTET_STREAM)
    public Boolean updateVehiclePhoto(@PathParam("vehicleId") Long vehicleId, byte[] image) throws AppException {
        return vehicleService.updateVehiclePhoto(vehicleId, image);
    }

    //删除车辆图片
    @DELETE
    @Path("{vehicleId}/photo")
    public Boolean deleteVehiclePhoto(@PathParam("vehicleId") Long vehicleId) throws AppException {
        return vehicleService.deleteVehiclePhoto(vehicleId);
    }

    //车辆进入
    @PUT
    @Path("{vehicleId}/entry")
    public Boolean vehicleEntry(@PathParam("vehicleId") Long vehicleId) throws AppException {
        return vehicleService.vehicleEntry(vehicleId);
    }

    //车辆离开
    @PUT
    @Path("{vehicleId}/leaving")
    public Boolean vehicleLeave(@PathParam("vehicleId") Long vehicleId) throws AppException {
        return vehicleService.vehicleLeave(vehicleId);
    }

    //外来车辆进出记录查询
    @GET
    @Path("entry-records")
    public Page<VehicleEntryRecordView> getVehicleEntryRecords(@QueryParam("vehicleId") Long vehicleId,
                                                               @QueryParam("vehicleSn") String vehicleSn,
                                                               @QueryParam("cardNumber") Long cardNumber,
                                                               @QueryParam("enabled") Integer enabled,
                                                               @QueryParam("outside") Integer outside,
                                                               @QueryParam("plateNumber") String plateNumber,
                                                               @QueryParam("driverName") String driverName,
                                                               @QueryParam("linkman") String linkman,
                                                               @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (vehicleId != null) {
            criteria.add(new Criterion("vehicleId", Criterion.Operator.EQ, vehicleId));
        }
        if (cardNumber != null) {
            criteria.add(new Criterion("cardNumber", Criterion.Operator.EQ, cardNumber));
        }
        if (enabled != null) {
            criteria.add(new Criterion("enabled", Criterion.Operator.EQ, enabled));
        }
        if (outside != null) {
            criteria.add(new Criterion("outside", Criterion.Operator.EQ, outside));
        }
        if (vehicleSn != null) {
            criteria.add(new Criterion("vehicleSn", Criterion.Operator.LIKE, '%' + vehicleSn + '%'));
        }
        if (plateNumber != null) {
            criteria.add(new Criterion("plateNumber", Criterion.Operator.LIKE, '%' + plateNumber + '%'));
        }
        if (driverName != null) {
            criteria.add(new Criterion("driverName", Criterion.Operator.LIKE, '%' + driverName + '%'));
        }
        if (linkman != null) {
            criteria.add(new Criterion("linkman", Criterion.Operator.LIKE, '%' + linkman + '%'));
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return vehicleService.getVehicleEntryRecords(criteria, pageRequest);
    }

    //查询车辆分布
    @GET
    @Path("distribution")
    public List<LocatorAssetCount> getVehicleDistribution(@QueryParam("floorNumber") Integer floorNumber) {

        return vehicleService.getVehicleDistribution(floorNumber);
    }

    //查询某个定位器下的车辆信息
    @GET
    @Path("under-locator/{locatorNumber}")
    public List<VehicleEntity> getVehicleUnderLocator(@PathParam("locatorNumber") Integer locatorNumber) {
        return vehicleService.getVehicleUnderLocator(locatorNumber);
    }

    //查询某个位置下的车辆信息
    @GET
    @Path("under-locator/{x},{y}")
    public List<VehicleEntity> getVehicleUnderLocator(@PathParam("x") double x, @PathParam("y") double y) {
        return vehicleService.getVehicleUnderPos(x, y);
    }

    //查询某个车辆轨迹
    @GET
    @Path("{vehicleId}/level3-move-locations")
    public List<VehicleMoveInLocationDto> getVehicleMoveLocationList(@PathParam("vehicleId") Long vehicleId,
                                                                     @QueryParam("startTime") Timestamp startTime,
                                                                     @QueryParam("endTime") Timestamp endTime) {
        return vehicleService.getVehicleMoveLocationList(vehicleId, startTime, endTime);
    }

    //根据车牌号查询车辆信息
    @GET
    @Path("get-vehicle-info")
    public List<VehicleInfoDto> getVehicleInfo(@QueryParam("plateNumber") String plateNumber){
        return vehicleService.getVehicleByPlateNumber(plateNumber);
    }
    //查询车辆实时定位
    @GET
    @Path("real-position/{fn}/{x1},{y1}-{x2},{y2}")
    public List<VehicleInfoDto> getPersonnelsInPolygon(@PathParam("fn") int floorNumber,
                                                       @PathParam("x1") double x1, @PathParam("y1") double y1,
                                                       @PathParam("x2") double x2, @PathParam("y2") double y2) {
        List<VehicleInfoDto> list =
                vehicleService.getVehicleInPolygon(floorNumber, GeomUtil.INSTANCE.createPolygon(x1, y1, x2, y2));
        return list;
    }
}
